
import { DataType } from '@zilliz/milvus2-sdk-node';
// import { milvusClient } from "ROOT/db/milvus";
import { MysqlDataSource } from "ROOT/db/orm";
import SymbolFactorEntity from "./SymbolFactor.entity";
import { outLogger } from 'ROOT/common/logger';
import { Period } from 'ROOT/trader';



class SymbolFactorService {
  private repository = MysqlDataSource.getRepository(SymbolFactorEntity);
  /**
   * 查询
   * @param {object} query
   */
  async find(query: { symbol: string, period?: Period }) {
    if (!query.period) {
      query.period = '4h'
    }
    const res = await this.repository.find({ where: query });
    return res;
  }

  /**
   * 查询单个
   * @param {object} query
   */
  async findOne(query: { symbol: string, time?: Date }) {
    const target = await this.repository.findOne({ where: query });
    return target;
  }
  /**
   * 查询个数
   * @param {object} query
   */
  async findCount(query: { symbol: string, period?: Period }) {
    const target = await this.repository.count({ where: query });
    return target;
  }

  /**
   * 创建
   * @param {object} query
   */
  async create(data: Omit<SymbolFactorEntity, 'id'>) {
    const target = await this.repository.save(data);
    return target;
  }

  /**
   * 更新
   * @param {object} query
   */
  async update(query: { symbol: string, time?: Date }, data: Partial<Omit<SymbolFactorEntity, 'id'>>) {
    const target = await this.findOne({ symbol: query.symbol, time: query.time })
    if (target) {
      Object.assign(target, data);
      this.repository.save(target);
    }
    return target;
  }

  /**
   * 删除1个
   * @param {object} query
   * @param { Document }
   */
  async deleteOne(query: Partial<Omit<SymbolFactorEntity, 'id'>>) {
    const target = await this.findOne({ symbol: query.symbol, time: query.time });

    if (target) {
      return await this.repository.remove(target);
    }
    return;
  }
  /**
   * 删除多个
   * @param {object} query
   * @param { Document }
   */
  async deleteMany(symbol: string, period: Period) {
    const list = await this.find({ symbol: symbol, period });
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      await this.repository.remove(element);
    }
  }
  async createCollection(symbol: string) {
    const COLLECTION_NAME = `${symbol}_vector`;
    
    // await milvusClient.dropCollection({
    //   collection_name: COLLECTION_NAME
    // })
    // const listCollections = await milvusClient.listCollections({collection_names: [COLLECTION_NAME]});
    // if (listCollections.data.length) {
    //   console.log(listCollections.data)
    //   return
    // }
    // const create = await milvusClient.createCollection({
    //   collection_name: COLLECTION_NAME,
    //   fields: [
    //     {
    //       name: 'id',
    //       description: 'ID field',
    //       data_type: DataType.Int64,
    //       is_primary_key: true,
    //       autoID: true,
    //     },
    //     {
    //       name: 'timestamp',
    //       description: 'timestamp',
    //       data_type: DataType.Int64,
    //     },
    //     {
    //       name: 'vector',
    //       description: 'Vector field',
    //       data_type: DataType.FloatVector,
    //       dim: 30,
    //     },
    //   ],
    // });
    // outLogger.info('Create collection is finished.', create);
    // return create
  }
  async insertVector(symbol: string, fields_data: { vector: number[], timestamp: number }[]) {

    // await milvusClient.use({ db_name: 'symbol_vectors' });
    // await this.createCollection(symbol)
    // const COLLECTION_NAME = `${symbol}_vector`;
    // console.log(fields_data[0].vector)
    // const insertResult = await milvusClient.insert({
    //   collection_name: COLLECTION_NAME,
    //   fields_data: fields_data,
    // });
    // outLogger.info('inserted', insertResult, fields_data[0].vector);

    // const createIndex = await milvusClient.createIndex({
    //   collection_name: COLLECTION_NAME,
    //   field_name: 'vector',
    //   metric_type: 'L2',
    // });
    // outLogger.info('Index is created', createIndex);
    // return insertResult;
  }

  async searchVector(symbol: string, vector: number[]) {
    const COLLECTION_NAME = `${symbol}_vector`;
    // need load collection before search
    // const load = await milvusClient.loadCollectionSync({
    //   collection_name: COLLECTION_NAME,
    // });
    // outLogger.info('Collection is loaded.', load);

    // const searchResult = await milvusClient.search({
    //   collection_name: COLLECTION_NAME,
    //   vector: vector, // 查询向量  
    //   output_fields: ['timestamp'],
    //   limit: 5,                  // 返回最相似的 top_k 个结果  
    // });
    // return searchResult;
  }
}

export default new SymbolFactorService();


